package com.dingreading.cloud.common.util.aliyun;


import com.dingreading.cloud.common.util.MD5Util;
import com.dingreading.cloud.common.util.RC4Util;

/**
 * 阿里云CDN工具
 */
public class CdnUtil {

    public final static String PROD_REFERER = "www.dingreading.com";
    public final static String TEST_REFERER = "test.dingreading.com";
    public final static String SCS_REFERER = "scs.dingreading.com";

    /**
     * URL鉴权方式A
     *
     * @param url 文件存储路劲，以/开头
     * @return ?auth_key=1703663821-0-0-450074a832c9e9ea50ae1a5821e7a81c
     */
    public static String getUrlAuthA(String url) {
        // 签算服务器生成鉴权URL的时间，精确到秒
        long timestamp = System.currentTimeMillis() / 1000;
        // 随机数，默认为0
        String rand = "0";
        // 用户ID，默认为0
        String uid = "0";
        // 鉴权密钥
        String privateKey = "dingreading";

        String rule = timestamp + "-" + rand + "-" + uid;
        // 生成规则：URI-Timestamp-rand-uid-PrivateKey
        String sstring = url + "-" + rule + "-" + privateKey;
        return "?auth_key=" + rule + "-" + MD5Util.string2MD5(sstring);
    }

    /**
     * 远程鉴权token
     */
    public static String getToken(String url, String ip, String expire, String cdnPrivateKey) {
        String baseToken = expire + "-" + ip + "-" + url;
        String token = RC4Util.encry_RC4_string(baseToken, cdnPrivateKey);
        return token;
    }


//    public static void main(String[] args) {
//        String s1 = getUrlAuthA("/493687a37b925b.jpg");
//        System.out.println("s1 = " + s1);
//        String s2 = getUrlAuthA("/transcode.mp4");
//        System.out.println("s2 = " + s2);
//        String s3 = getUrlAuthA("/xiaozhangbao.zip");
//        System.out.println("s3 = " + s3);
//        String s3 = getUrlAuthA("/store/20220622/0C9861581E9642D1BAA0D2B85FE9683A.jpeg");
//        System.out.println("s3 = " + s3);
//    }

}
